package com.cbry.easyddl.gui;

import cn.hutool.core.util.StrUtil;
import com.cbry.easyddl.util.JavaToMySQLTypeConverter;
import com.intellij.openapi.ui.Messages;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author cbry
 * @version 1.0.0
 * @ClassName CsvWindow
 * @createTime 2024年05月01日 20:06:55
 */
public class CsvWindow {
    private JPanel panel1;
    private JTextPane csvTextPanel;
    private JButton genButton;
    private JButton importExcelButton;
    private JLabel jTitle;
    private JPanel buttomSpacePanel;
    private JTextField csvSplider;
    private JTextPane resPanel;

    public CsvWindow() {
        genButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String splider = csvSplider.getText();
                String str = csvTextPanel.getText();
                String[] rowArr = str.split("\n");
                String primaryKey="";

                StringBuilder ddl = new StringBuilder("CREATE TABLE " + StrUtil.toUnderlineCase("default_table") + " (\n");

                for (String row : rowArr) {
                    String[] ele = row.split(splider);
                    primaryKey = "".equals(primaryKey) ? ele[0] :primaryKey ;
                    ddl.append("  ").append('`' +ele[0]+ '`').append("  " + JavaToMySQLTypeConverter.convertJavaToMySQLType(ele[1]) + ",\n");
                }
                ddl.append("  PRIMARY KEY (");
                ddl.append(primaryKey) // 假设第一个字段是主键
                        .append(")\n");
                ddl.append(");");
                resPanel.setText(ddl.toString());
                // Messages.showMessageDialog("生成"+ csvTextPanel.getText(), "测试", Messages.getInformationIcon());
            }
        });
        importExcelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Messages.showMessageDialog("导入excel", "测试", Messages.getInformationIcon());
            }
        });
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here

    }

    public JPanel getMainPanel() {
        return panel1;
    }
}
